package pure.ui 
{
	import pure.ui.core.Component;
	import pure.ui.core.IPopUp;
	import pure.ui.skins.ISkin;
	import pure.ui.skins.WindowSkin;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	/**
	 * 窗口
	 * @author Des
	 * <皮肤种类> ---- WindowSkin
	 * 
	 */
	
public class Window extends Panel implements IPopUp
{
	public function Window(x:Number, y:Number, size:Point = null, titleSize:Point = null, skin:ISkin = null, autoTitleCenter:Boolean = false, dragging:Boolean = true)
	{
		_autoTitleCenter = autoTitleCenter;
		if (titleSize)
		{
			_title = new Image(0, 0, titleSize, null, true,true);
		}
		else
		{
			_title = new Image(0, 0, new Point(TITLE_WIDTH,TITLE_HEIGHT), null, true,true);
		}
		this.dragging = dragging;
		super(x, y, size, skin);
		addChild(_title);	
	}
	
	
	
	/*default*/
	
	protected static const TITLE_WIDTH:Number = 170;
	
	protected static const TITLE_HEIGHT:Number = 30;
		
	/*model*/
	
	protected var _dragging:Boolean;
	
	/*cache*/
	
	protected var _title:Image;	// 标题框
	
	protected var _autoTitleCenter:Boolean;	// 标题框是否垂直居中(否则为居上)
	
	protected var _oldMouseX:Number;
	
	protected var _oldMouseY:Number;
	
	
	
	/**
	 * 标题框大小
	 * @param w			标题框宽度
	 * @param h			标题框高度
	 **/
	public function setTitleSize(w:Number, h:Number):void
	{
		_title.setSize(w, h);
	}
	
	/**
	 * 毁灭
	 **/
	override public function destroy():void
	{
		_title.destroy();
		if (_dragging)
		{
			_title.removeEventListener(MouseEvent.MOUSE_DOWN, ____onMouseDown);
			____onMouseUp();
		}
		super.destroy();
	}

	
	
	
	
	/* ----------------------------------------------------    get / set    ----------------------------------------------------*/
	
	
	
	
	
	// 窗口皮肤
	override public function set skin(value:ISkin):void
	{
		if (!value)
		{
			return;
		}
		if (!value is WindowSkin)
		{
			throw new Error('Window::skin - 皮肤类型错误！');
		}
		this._skin = WindowSkin(value).content;
		_title.skin = WindowSkin(value).title;
		update();
	}	
	
	// 是否可拖拽
	public function set dragging(b:Boolean):void
	{
		if (_dragging != b)
		{
			_dragging = b;
			_setDrag();
		}
	}
	
	public function get dragging():Boolean
	{
		return _dragging;
	}
	
	// 标题框
	public function get title():Image
	{
		return _title;
	}
	
	
	
	
	
	/* ----------------------------------------------------    private    ----------------------------------------------------*/
	
	
	
	

	/**
	 * 绘制
	 **/
	override protected function _draw():void
	{
		super._draw();
		_rejustTitlePosition();
	}
	
	/**
	 * 调整标题框位置
	 **/
	protected function _rejustTitlePosition():void
	{
		_title.x = (this._width - this._title.width) * 0.5;
		_title.y = this._autoTitleCenter? -_title.height * 0.5: -_title.height;
	}
	
	/**
	 * 设置拖拽
	 **/
	protected function _setDrag():void
	{
		if (_dragging)
		{
			_title.addEventListener(MouseEvent.MOUSE_DOWN, ____onMouseDown);
		}
		else
		{
			_title.removeEventListener(MouseEvent.MOUSE_DOWN, ____onMouseDown);
		}
	}
	
	/**
	 * 按住拖拽
	 **/
	protected function ____onMouseDown(e:MouseEvent):void
	{
		_title.addEventListener(MouseEvent.MOUSE_UP, ____onMouseUp);
		stage.addEventListener(MouseEvent.MOUSE_MOVE, ____onMouseMove);
		_oldMouseX = stage.mouseX;
		_oldMouseY = stage.mouseY;
	}
	
	/**
	 * 移动
	 **/
	protected function ____onMouseMove(e:MouseEvent):void
	{
		e.updateAfterEvent();
		if (!_checkMouseOnStage())
		{
			____onMouseUp();
		}
		var offsetX:Number = stage.mouseX - _oldMouseX;
		var offsetY:Number = stage.mouseY - _oldMouseY;
		this.x += offsetX;
		this.y += offsetY;
		_oldMouseX = stage.mouseX;
		_oldMouseY = stage.mouseY;
	}
	
	/**
	 * 放开
	 **/
	protected function ____onMouseUp(e:MouseEvent = null):void
	{
		_title.removeEventListener(MouseEvent.MOUSE_UP, ____onMouseUp);
		stage.removeEventListener(MouseEvent.MOUSE_MOVE, ____onMouseMove);
	}
	
	/**
	 * 判断滑鼠是否位于舞台内
	 **/
	protected function _checkMouseOnStage():Boolean
	{
		var mouseX:Number = stage.mouseX;
		if (mouseX<0||mouseX>stage.stageWidth)
		{
			return false
		}
		var mouseY:Number = stage.mouseY;
		if (mouseY<0||mouseY>stage.stageHeight)
		{
			return false;
		}
		return true;
	}
}

}